﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
    [Serializable()]
    public class KBDieuDuongBO : Csla.BusinessBase<KBDieuDuongBO>
    {
        #region Business Properties

        private int? _KBDieuDuongID = null;
        public int? KBDieuDuongID
        {
            get { return this._KBDieuDuongID; }
            set { this._KBDieuDuongID = value; PropertyHasChanged("KBDieuDuongID"); }
        }

        private int? _Status = 2;
        public int? Status
        {
            get { return this._Status; }
            set { this._Status = value; PropertyHasChanged("Status"); }
        }

        private int? _BenhNhanID = null;
        public int? BenhNhanID
        {
            get { return this._BenhNhanID; }
            set { this._BenhNhanID = value; PropertyHasChanged("BenhNhanID"); }
        }

        private DateTime? _Ngay = null;
        public DateTime? Ngay
        {
            get { return this._Ngay; }
            set { this._Ngay = value; PropertyHasChanged("Ngay"); }
        }

        private int? _DieuDuongID = null;
        public int? DieuDuongID
        {
            get { return this._DieuDuongID; }
            set { this._DieuDuongID = value; PropertyHasChanged("DieuDuongID"); }
        }

        private string _ToanThan = null;
        public string ToanThan
        {
            get { return this._ToanThan; }
            set { this._ToanThan = value; PropertyHasChanged("ToanThan"); }
        }

        private double? _Mach = null;
        public double? Mach
        {
            get { return this._Mach; }
            set { this._Mach = value; PropertyHasChanged("Mach"); }
        }

        private double? _NhietDo = null;
        public double? NhietDo
        {
            get { return this._NhietDo; }
            set { this._NhietDo = value; PropertyHasChanged("NhietDo"); }
        }

        private double? _HuyetAp1 = null;
        public double? HuyetAp1
        {
            get { return this._HuyetAp1; }
            set { this._HuyetAp1 = value; PropertyHasChanged("HuyetAp1"); }
        }

        private double? _HuyetAp2 = null;
        public double? HuyetAp2
        {
            get { return this._HuyetAp2; }
            set { this._HuyetAp2 = value; PropertyHasChanged("HuyetAp2"); }
        }

        private double? _NhipTho = null;
        public double? NhipTho
        {
            get { return this._NhipTho; }
            set { this._NhipTho = value; PropertyHasChanged("NhipTho"); }
        }

        private double? _CanNang = null;
        public double? CanNang
        {
            get { return this._CanNang; }
            set { this._CanNang = value; PropertyHasChanged("CanNang"); }
        }

        private double? _ChieuCao = null;
        public double? ChieuCao
        {
            get { return this._ChieuCao; }
            set { this._ChieuCao = value; PropertyHasChanged("ChieuCao"); }
        }

        private string _TuanHoan = null;
        public string TuanHoan
        {
            get { return this._TuanHoan; }
            set { this._TuanHoan = value; PropertyHasChanged("TuanHoan"); }
        }

        private string _HoHap = null;
        public string HoHap
        {
            get { return this._HoHap; }
            set { this._HoHap = value; PropertyHasChanged("HoHap"); }
        }

        private string _TieuHoa = null;
        public string TieuHoa
        {
            get { return this._TieuHoa; }
            set { this._TieuHoa = value; PropertyHasChanged("TieuHoa"); }
        }

        private string _ThanTietNieuSinhDuc = null;
        public string ThanTietNieuSinhDuc
        {
            get { return this._ThanTietNieuSinhDuc; }
            set { this._ThanTietNieuSinhDuc = value; PropertyHasChanged("ThanTietNieuSinhDuc"); }
        }

        private string _ThanKinh = null;
        public string ThanKinh
        {
            get { return this._ThanKinh; }
            set { this._ThanKinh = value; PropertyHasChanged("ThanKinh"); }
        }

        private string _CoXuongKhop = null;
        public string CoXuongKhop
        {
            get { return this._CoXuongKhop; }
            set { this._CoXuongKhop = value; PropertyHasChanged("CoXuongKhop"); }
        }

        private string _TaiMuiHong = null;
        public string TaiMuiHong
        {
            get { return this._TaiMuiHong; }
            set { this._TaiMuiHong = value; PropertyHasChanged("TaiMuiHong"); }
        }

        private string _RangHamMat = null;
        public string RangHamMat
        {
            get { return this._RangHamMat; }
            set { this._RangHamMat = value; PropertyHasChanged("RangHamMat"); }
        }

        private string _Mat = null;
        public string Mat
        {
            get { return this._Mat; }
            set { this._Mat = value; PropertyHasChanged("Mat"); }
        }

        private string _NoiTietDinhDuong = null;
        public string NoiTietDinhDuong
        {
            get { return this._NoiTietDinhDuong; }
            set { this._NoiTietDinhDuong = value; PropertyHasChanged("NoiTietDinhDuong"); }
        }

        private int? _UserCreated = Session.LoginID;
        public int? UserCreated
        {
            get { return this._UserCreated; }
            set { this._UserCreated = value; PropertyHasChanged("UserCreated"); }
        }

        private DateTime? _DateCreated = DateTime.MaxValue;
        public DateTime? DateCreated
        {
            get { return this._DateCreated; }
            set { this._DateCreated = value; PropertyHasChanged("DateCreated"); }
        }

        private int? _UserModify = null;
        public int? UserModify
        {
            get { return this._UserModify; }
            set { this._UserModify = value; PropertyHasChanged("UserModify"); }
        }

        private DateTime? _DateModify = null;
        public DateTime? DateModify
        {
            get { return this._DateModify; }
            set { this._DateModify = value; PropertyHasChanged("DateModify"); }
        }

        private bool? _Deleted = false;
        public bool? Deleted
        {
            get { return this._Deleted; }
            set { this._Deleted = value; PropertyHasChanged("Deleted"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _KBDieuDuongID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public void SetIsChild()
        {
            MarkAsChild();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newKBDieuDuongID = -1;
        private KBDieuDuongBO()
        {
            _KBDieuDuongID = newKBDieuDuongID--;
        }

        public KBDieuDuongBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public KBDieuDuongBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static KBDieuDuongBO NewRootKBDieuDuongBO()
        {
            return DataPortal.Create<KBDieuDuongBO>(new RootCriteria());
        }

        internal static KBDieuDuongBO NewChildKBDieuDuongBO()
        {
            return DataPortal.Create<KBDieuDuongBO>(new ChildCriteria());
        }

        public static KBDieuDuongBO GetRootKBDieuDuongBO(int _KBDieuDuongID)
        {
            return DataPortal.Fetch<KBDieuDuongBO>(new RootCriteria(_KBDieuDuongID));
        }

        public static KBDieuDuongBO GetRootKBDieuDuongBOByWhere(string where)
        {
            return DataPortal.Fetch<KBDieuDuongBO>(where);
        }

        internal static KBDieuDuongBO GetChildKBDieuDuongBO(SqlDataReader dataReader)
        {
            return new KBDieuDuongBO(dataReader);
        }

        public static void DeleteKBDieuDuongBO(int _KBDieuDuongID)
        {
            DataPortal.Delete(new RootCriteria(_KBDieuDuongID));
        }

        public static void DeleteKBDieuDuongBO(int _KBDieuDuongID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_KBDieuDuongID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int KBDieuDuongID;
            public bool directDelete = false;
            public RootCriteria(int _KBDieuDuongID)
            {
                this.KBDieuDuongID = _KBDieuDuongID;
                this.directDelete = false;
            }

            public RootCriteria(int _KBDieuDuongID, bool directDelete)
            {
                this.KBDieuDuongID = _KBDieuDuongID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int KBDieuDuongID;
            public bool directDelete = false;
            public ChildCriteria(int _KBDieuDuongID)
            {
                this.KBDieuDuongID = _KBDieuDuongID;
                this.directDelete = false;
            }

            public ChildCriteria(int _KBDieuDuongID, bool directDelete)
            {
                this.KBDieuDuongID = _KBDieuDuongID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBDieuDuongBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[KBDieuDuongID] = " + criteria.KBDieuDuongID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBDieuDuongBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["KBDieuDuongID"] is DBNull))
                this._KBDieuDuongID = (int)dataReader["KBDieuDuongID"];
            if (!(dataReader["Status"] is DBNull))
                this._Status = (int)dataReader["Status"];
            if (!(dataReader["BenhNhanID"] is DBNull))
                this._BenhNhanID = (int)dataReader["BenhNhanID"];
            if (!(dataReader["Ngay"] is DBNull))
                this._Ngay = (DateTime)dataReader["Ngay"];
            if (!(dataReader["DieuDuongID"] is DBNull))
                this._DieuDuongID = (int)dataReader["DieuDuongID"];
            if (!(dataReader["ToanThan"] is DBNull))
                this._ToanThan = (string)dataReader["ToanThan"];
            if (!(dataReader["Mach"] is DBNull))
                this._Mach = (double)dataReader["Mach"];
            if (!(dataReader["NhietDo"] is DBNull))
                this._NhietDo = (double)dataReader["NhietDo"];
            if (!(dataReader["HuyetAp1"] is DBNull))
                this._HuyetAp1 = (double)dataReader["HuyetAp1"];
            if (!(dataReader["HuyetAp2"] is DBNull))
                this._HuyetAp2 = (double)dataReader["HuyetAp2"];
            if (!(dataReader["NhipTho"] is DBNull))
                this._NhipTho = (double)dataReader["NhipTho"];
            if (!(dataReader["CanNang"] is DBNull))
                this._CanNang = (double)dataReader["CanNang"];
            if (!(dataReader["ChieuCao"] is DBNull))
                this._ChieuCao = (double)dataReader["ChieuCao"];
            if (!(dataReader["TuanHoan"] is DBNull))
                this._TuanHoan = (string)dataReader["TuanHoan"];
            if (!(dataReader["HoHap"] is DBNull))
                this._HoHap = (string)dataReader["HoHap"];
            if (!(dataReader["TieuHoa"] is DBNull))
                this._TieuHoa = (string)dataReader["TieuHoa"];
            if (!(dataReader["ThanTietNieuSinhDuc"] is DBNull))
                this._ThanTietNieuSinhDuc = (string)dataReader["ThanTietNieuSinhDuc"];
            if (!(dataReader["ThanKinh"] is DBNull))
                this._ThanKinh = (string)dataReader["ThanKinh"];
            if (!(dataReader["CoXuongKhop"] is DBNull))
                this._CoXuongKhop = (string)dataReader["CoXuongKhop"];
            if (!(dataReader["TaiMuiHong"] is DBNull))
                this._TaiMuiHong = (string)dataReader["TaiMuiHong"];
            if (!(dataReader["RangHamMat"] is DBNull))
                this._RangHamMat = (string)dataReader["RangHamMat"];
            if (!(dataReader["Mat"] is DBNull))
                this._Mat = (string)dataReader["Mat"];
            if (!(dataReader["NoiTietDinhDuong"] is DBNull))
                this._NoiTietDinhDuong = (string)dataReader["NoiTietDinhDuong"];
            if (!(dataReader["UserCreated"] is DBNull))
                this._UserCreated = (int)dataReader["UserCreated"];
            if (!(dataReader["DateCreated"] is DBNull))
                this._DateCreated = (DateTime)dataReader["DateCreated"];
            if (!(dataReader["UserModify"] is DBNull))
                this._UserModify = (int)dataReader["UserModify"];
            if (!(dataReader["DateModify"] is DBNull))
                this._DateModify = (DateTime)dataReader["DateModify"];
            if (!(dataReader["Deleted"] is DBNull))
                this._Deleted = (bool)dataReader["Deleted"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._UserCreated = Session.LoginID;
            this._DateCreated = DateTime.MaxValue;
            this._UserModify = null;
            this._DateModify = null;
            this._Deleted = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBDieuDuongBO_InsertBO");
            db.AddInParameter(cmd, "@Status", SqlDbType.Int, this._Status);
            db.AddInParameter(cmd, "@BenhNhanID", SqlDbType.Int, this._BenhNhanID);
            db.AddInParameter(cmd, "@Ngay", SqlDbType.DateTime, this._Ngay);
            db.AddInParameter(cmd, "@DieuDuongID", SqlDbType.Int, this._DieuDuongID);
            db.AddInParameter(cmd, "@ToanThan", SqlDbType.NVarChar, this._ToanThan);
            db.AddInParameter(cmd, "@Mach", SqlDbType.Float, this._Mach);
            db.AddInParameter(cmd, "@NhietDo", SqlDbType.Float, this._NhietDo);
            db.AddInParameter(cmd, "@HuyetAp1", SqlDbType.Float, this._HuyetAp1);
            db.AddInParameter(cmd, "@HuyetAp2", SqlDbType.Float, this._HuyetAp2);
            db.AddInParameter(cmd, "@NhipTho", SqlDbType.Float, this._NhipTho);
            db.AddInParameter(cmd, "@CanNang", SqlDbType.Float, this._CanNang);
            db.AddInParameter(cmd, "@ChieuCao", SqlDbType.Float, this._ChieuCao);
            db.AddInParameter(cmd, "@TuanHoan", SqlDbType.NVarChar, this._TuanHoan);
            db.AddInParameter(cmd, "@HoHap", SqlDbType.NVarChar, this._HoHap);
            db.AddInParameter(cmd, "@TieuHoa", SqlDbType.NVarChar, this._TieuHoa);
            db.AddInParameter(cmd, "@ThanTietNieuSinhDuc", SqlDbType.NVarChar, this._ThanTietNieuSinhDuc);
            db.AddInParameter(cmd, "@ThanKinh", SqlDbType.NVarChar, this._ThanKinh);
            db.AddInParameter(cmd, "@CoXuongKhop", SqlDbType.NVarChar, this._CoXuongKhop);
            db.AddInParameter(cmd, "@TaiMuiHong", SqlDbType.NVarChar, this._TaiMuiHong);
            db.AddInParameter(cmd, "@RangHamMat", SqlDbType.NVarChar, this._RangHamMat);
            db.AddInParameter(cmd, "@Mat", SqlDbType.NVarChar, this._Mat);
            db.AddInParameter(cmd, "@NoiTietDinhDuong", SqlDbType.NVarChar, this._NoiTietDinhDuong);
            db.AddInParameter(cmd, "@UserCreated", SqlDbType.Int, this._UserCreated);
            db.AddOutParameter(cmd, "@NewKBDieuDuongID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDateCreated", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._KBDieuDuongID = (int)cmd.Parameters["@NewKBDieuDuongID"].Value;
            this._DateCreated = (DateTime)cmd.Parameters["@NewDateCreated"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._UserModify = Session.LoginID;
            this._DateModify = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBDieuDuongBO_UpdateBO");
            db.AddInParameter(cmd, "@KBDieuDuongID", SqlDbType.Int, this._KBDieuDuongID);
            db.AddInParameter(cmd, "@Status", SqlDbType.Int, this._Status);
            db.AddInParameter(cmd, "@BenhNhanID", SqlDbType.Int, this._BenhNhanID);
            db.AddInParameter(cmd, "@Ngay", SqlDbType.DateTime, this._Ngay);
            db.AddInParameter(cmd, "@DieuDuongID", SqlDbType.Int, this._DieuDuongID);
            db.AddInParameter(cmd, "@ToanThan", SqlDbType.NVarChar, this._ToanThan);
            db.AddInParameter(cmd, "@Mach", SqlDbType.Float, this._Mach);
            db.AddInParameter(cmd, "@NhietDo", SqlDbType.Float, this._NhietDo);
            db.AddInParameter(cmd, "@HuyetAp1", SqlDbType.Float, this._HuyetAp1);
            db.AddInParameter(cmd, "@HuyetAp2", SqlDbType.Float, this._HuyetAp2);
            db.AddInParameter(cmd, "@NhipTho", SqlDbType.Float, this._NhipTho);
            db.AddInParameter(cmd, "@CanNang", SqlDbType.Float, this._CanNang);
            db.AddInParameter(cmd, "@ChieuCao", SqlDbType.Float, this._ChieuCao);
            db.AddInParameter(cmd, "@TuanHoan", SqlDbType.NVarChar, this._TuanHoan);
            db.AddInParameter(cmd, "@HoHap", SqlDbType.NVarChar, this._HoHap);
            db.AddInParameter(cmd, "@TieuHoa", SqlDbType.NVarChar, this._TieuHoa);
            db.AddInParameter(cmd, "@ThanTietNieuSinhDuc", SqlDbType.NVarChar, this._ThanTietNieuSinhDuc);
            db.AddInParameter(cmd, "@ThanKinh", SqlDbType.NVarChar, this._ThanKinh);
            db.AddInParameter(cmd, "@CoXuongKhop", SqlDbType.NVarChar, this._CoXuongKhop);
            db.AddInParameter(cmd, "@TaiMuiHong", SqlDbType.NVarChar, this._TaiMuiHong);
            db.AddInParameter(cmd, "@RangHamMat", SqlDbType.NVarChar, this._RangHamMat);
            db.AddInParameter(cmd, "@Mat", SqlDbType.NVarChar, this._Mat);
            db.AddInParameter(cmd, "@NoiTietDinhDuong", SqlDbType.NVarChar, this._NoiTietDinhDuong);
            db.AddInParameter(cmd, "@UserModify", SqlDbType.Int, this._UserModify);
            db.AddInParameter(cmd, "@Deleted", SqlDbType.Bit, this._Deleted);

            db.AddOutParameter(cmd, "@NewDateModify", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DateModify = (DateTime)cmd.Parameters["@NewDateModify"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_KBDieuDuongID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBDieuDuongBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@KBDieuDuongID", SqlDbType.Int, criteria.KBDieuDuongID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_KBDieuDuongID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
